<?php
defined('IN_IA') or exit('Access Denied');
wl_load()->model('merchant');
wl_load()->model('judgment');
wl_load()->func('print');
$ops = array('master_refund', 'import', 'modifyPrice', 'refunds', 'dayin', 'update', 'summary', 'received', 'detail', 'judgment', 'output', 'outputSNG', 'remark', 'address', 'confrimpay', 'confirmsend', 'cancelsend', 'refund', 'updaterefund');
$op_names = array('退款', '打印', '修改价格', '订单概况', '订单列表', '订单详情', '导出', '卖家备注', '修改收货地址', '确认付款', '发货', '取消发货', '退款');
foreach ($ops as $key => $value) {
    permissions('do', 'ac', 'op', 'order', 'order', $ops[$key], '订单', '订单管理', $op_names[$key]);
}
$op = in_array($op, $ops) ? $op : 'summary';
// TODO 添加是否减过库存字段
if (!pdo_fieldexists('tg_order', 'is_stock')) {
    pdo_query("ALTER TABLE " . tablename('tg_order') . " ADD `is_stock` int(3) not null default 1 COMMENT '是否减过库存';");
}
$gettime = $this->module['config']['gettime'];//自动签收时间
$uniacid = $_W['uniacid'];
//$merchants = pdo_fetchall("SELECT * FROM " . tablename('tg_merchant') . " WHERE uniacid = '{$_W['uniacid']}'  ORDER BY id DESC");
session_start();
$dispatchtype = $_GPC['dispatchtype'];
if (!empty($_GPC['dispatchtype'])) {
    $_SESSION['dispatchtype'] = $_GPC['dispatchtype'];
    $dispatchtype = $_SESSION['dispatchtype'];
}
if ($op == 'dayin') {
    $id = intval($_GPC['id']);
    tuan_print($id);


    message('打印成功');

}
if ($op == 'update') {
    $id = intval($_GPC['id']);
    $item = pdo_fetch("SELECT * FROM " . tablename('tg_order') . " WHERE id = :id", array(':id' => $id));
    if (!empty($_GPC['price'])) {

        pdo_update('tg_order', array('price' => $_GPC['price']), array('id' => $id));

    }
}
if ($op == 'updaterefund') {
    $goodsid = intval($_GPC['goodsid2']);
    if ($goodsid == 0) {
        message('请先选择商品，并搜索后,才可变更订单状态为待退款');
    }
    pdo_update('tg_order', array('status' => 10), array('g_id' => $goodsid, 'status' => 1));
    pdo_update('tg_order', array('status' => 10), array('g_id' => $goodsid, 'status' => 2));
    pdo_update('tg_order', array('status' => 10), array('g_id' => $goodsid, 'status' => 8));
    $op = 'received';
}
if ($op == 'modifyPrice') {
    $id = intval($_GPC['id']);
    $item = pdo_fetch("SELECT * FROM " . tablename('tg_order') . " WHERE id = :id", array(':id' => $id));
    if (!empty($_GPC['price'])) {
        pdo_update('tg_order', array('pay_price' => $_GPC['price']), array('id' => $id));
        pdo_update('core_paylog', array('fee' => $_GPC['price']), array('tid' => $item['orderno']));
    }
    echo 1;
    exit;
}
if ($op == 'summary') {
    $seven_orders = 0;
    $seven_nocash_orders = 0;
    $obligations = 0;
    $undelivereds = 0;
    $incomes = 0;
    $yesterday_orders = 0;
    $yesterday_payorder = 0;
    $yesterday_obligation = 0;
    $stime = strtotime(date('Y-m-d')) - 6 * 86400;

    $etime = strtotime(date('Y-m-d')) + 86400;
    $yetime = strtotime(date('Y-m-d'));
    $ytime = strtotime(date('Y-m-d')) - 86400;
    $seven_orders = pdo_fetchcolumn("SELECT COUNT(id) FROM " . tablename('tg_order') . " WHERE uniacid = {$_W['uniacid']}    and createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $stime, ':endtime' => $etime));
    $seven_nocash_orders = pdo_fetchcolumn("SELECT COUNT(id) FROM " . tablename('tg_order') . " WHERE uniacid = {$_W['uniacid']}  and status=0   and createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $stime, ':endtime' => $etime));
    $obligations = pdo_fetchcolumn("SELECT COUNT(id) FROM " . tablename('tg_order') . " WHERE uniacid = {$_W['uniacid']}  and status in (1,2,3,4,5,6,7,8)   and createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $stime, ':endtime' => $etime));
    $undelivereds = pdo_fetchcolumn("SELECT COUNT(id) FROM " . tablename('tg_order') . " WHERE uniacid = {$_W['uniacid']}  and status in (4,6,7)   and createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $stime, ':endtime' => $etime));

    $seven = pdo_fetchcolumn("select Sum(pay_price) as totalprice  from" . tablename('tg_order') . " WHERE uniacid = '{$_W['uniacid']}' and mobile<>'虚拟' and status in(1,2,3,4,5,6,7,8,10)  AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $stime, ':endtime' => $etime));
    /*foreach($seven as$key=>$value){
        $incomes += $value['pay_price'];
    }*/
    $incomes = $seven;
    $yesterday_orders_list = pdo_fetchall("SELECT id,status FROM " . tablename('tg_order') . " WHERE uniacid = '{$_W['uniacid']}' and mobile<>'虚拟'   AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $ytime, ':endtime' => $yetime));
    $yesterday_payorder = 0;
    $yesterday_obligation = 0;
    foreach ($yesterday_orders_list as $item) {
        if ($item['status'] == 1 || $item['status'] == 2 || $item['status'] == 3 || $item['status'] == 4 || $item['status'] == 6 || $item['status'] == 7) {
            $yesterday_payorder += 1;
        }
        if ($item['status'] == 3) {
            $yesterday_obligation += 1;
        }
    }
    $yesterday_orders = count($yesterday_orders_list);
    /*
    $yesterday_orders=pdo_fetchcolumn("SELECT COUNT(id) FROM " . tablename('tg_order') . " WHERE uniacid = '{$_W['uniacid']}' and mobile<>'虚拟'   AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array( ':createtime' => $ytime, ':endtime' => $yetime));
    $yesterday_payorder = pdo_fetchcolumn("SELECT COUNT(id) FROM " . tablename('tg_order') . " WHERE uniacid = '{$_W['uniacid']}' and mobile<>'虚拟' and status in(1,2,3,4,6,7)   AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array( ':createtime' => $ytime, ':endtime' => $yetime));
    $yesterday_obligation = pdo_fetchcolumn("SELECT COUNT(id) FROM " . tablename('tg_order') . " WHERE uniacid = '{$_W['uniacid']}' and mobile<>'虚拟' and status = 3   AND sendtime >= :createtime AND sendtime <= :endtime ORDER BY createtime ASC", array( ':createtime' => $ytime, ':endtime' => $yetime));
*/
    $con = "uniacid = '{$_W['uniacid']}' and mobile<>'虚拟' ";
    $starttime = empty($_GPC['time']['start']) ? $stime : strtotime($_GPC['time']['start']);
    $endtime = empty($_GPC['time']['end']) ? $etime : strtotime($_GPC['time']['end']) + 86400;
    $s = $starttime;
    $e = $endtime;
    $list = array();
    $j = 0;

    while ($e >= $s) {
        $listone = pdo_fetchcolumn("SELECT COUNT(id)  FROM " . tablename('tg_order') . " WHERE $con   AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $e - 86400, ':endtime' => $e));
        $status1 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $con and status  in (1,2,3,4,5,6,7,8,10)   AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $e - 86400, ':endtime' => $e));
        $status4 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $con and status=3   AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $e - 86400, ':endtime' => $e));
        $status2 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $con and status in (4,5,6,7,10)   AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':createtime' => $e - 86400, ':endtime' => $e));

        $list[$j]['gnum'] = $listone;
        $list[$j]['status4'] = $status4;
        $list[$j]['status1'] = $status1;
        $list[$j]['status2'] = $status2;
        $list[$j]['createtime'] = $e - 86400;
        $j++;
        $e = $e - 86400;
    }

    $day = $hit = $status4 = $status1 = $status2 = array();
    if (!empty($list)) {
        foreach ($list as $row) {
            $day[] = date('m-d', $row['createtime']);
            $hit[] = intval($row['gnum']);
            $status4[] = intval($row['status4']);
            $status1[] = intval($row['status1']);
            $status2[] = intval($row['status2']);
        }
    }

    for ($i = 0; $i = count($hit) < 2; $i++) {
        $day[] = date('m-d', $endtime);
        $hit[] = $day[$i] == date('m-d', $endtime) ? $hit[0] : '0';
    }
    include wl_template('order/summary');
    exit;
}

if ($op == 'received') {
    $allgoods = pdo_fetchall("select gname,id from " . tablename('tg_goods') . " where uniacid=:uniacid order by id desc ", array(':uniacid' => $_W['uniacid']));
    $pindex = max(1, intval($_GPC['page']));
    $psize = 10;
    $condition = "  uniacid = '{$_W['uniacid']}'";
    $condition2 = "  uniacid = '{$_W['uniacid']}'";
    $paras = array(':uniacid' => $_W['uniacid']);
    $godluck = $_GPC['godluck'];
    $issued = $_GPC['issued'];
    $salers = $_GPC['salers'];
    $status = $_GPC['status'];
    $transid = $_GPC['transid'];
    $pay_type = $_GPC['pay_type'];
    $keyword = $_GPC['keyword'];
    $member = $_GPC['member'];
    $time = $_GPC['time'];
    $idcard = $_GPC['idcard'];

    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-7 day');
        $endtime = time();
    }

    if (!empty($_GPC['time'])) {
        //message($_GPC['time']['start']);
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);
        $condition .= " AND  createtime >= {$starttime} AND  createtime <= {$endtime} ";
        $condition2 .= " AND  createtime >= {$starttime} AND  createtime <= {$endtime} ";
        $paras[':starttime'] = $starttime;
        $paras[':endtime'] = $endtime;
    }
    if (trim($_GPC['goodsid']) != '') {
        $condition .= " and (g_id = '{$_GPC['goodsid']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid']}')) ";
        $condition2 .= " and g_id like '%{$_GPC['goodsid']}%' ";;
    }
    if (trim($_GPC['goodsid2']) != '') {
        $condition .= " and (g_id = '{$_GPC['goodsid2']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid2']}')) ";
        $condition2 .= " and (g_id = '{$_GPC['goodsid2']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid2']}')) ";

        //$condition .= " and g_id like '%{$_GPC['goodsid2']}%' ";
        //$condition2 .= " and g_id like '%{$_GPC['goodsid2']}%' ";
    }
    if (trim($_GPC['address']) != '') {

        $condition .= " and address like '%{$_GPC['address']}%' ";
        $condition2 .= " and address like '%{$_GPC['address']}%' ";
    }

    if (!empty($_GPC['transid'])) {
        $condition .= " AND  transid  like '%{$_GPC['transid']}%' ";
        $condition2 .= " AND  transid  like '%{$_GPC['transid']}%' ";
    }
    if (!empty($_GPC['pay_type'])) {
        $condition .= " AND  pay_type = '{$_GPC['pay_type']}'";
        $condition2 .= " AND  pay_type = '{$_GPC['pay_type']}'";
    } elseif ($_GPC['pay_type'] === '0') {
        $condition .= " AND  pay_type = '{$_GPC['pay_type']}'";
        $condition2 .= " AND  pay_type = '{$_GPC['pay_type']}'";
    }
    if (!empty($_GPC['keyword'])) {
        $condition .= " AND  orderno LIKE '%{$_GPC['keyword']}%'";
        $condition2 .= " AND  orderno LIKE '%{$_GPC['keyword']}%'";
    }
    if (!empty($_GPC['member'])) {
        $condition .= " AND addname = '{$_GPC['member']}' ";
        $condition2 .= " AND addname = '{$_GPC['member']}'";
    }
    if (!empty($_GPC['mobile'])) {
        $condition .= " AND  mobile = '{$_GPC['mobile']}' ";
        $condition2 .= " AND  mobile = '{$_GPC['mobile']}'";
    }

    if (!empty($_GPC['idcard'])) {
        $condition .= " and openid in ( select from_user from " . tablename('tg_member') . " where uniacid = '{$_W['uniacid']}' and idcard like '%{$_GPC['idcard']}%' )";
        $condition2 .= " and openid in ( select from_user from " . tablename('tg_member') . " where uniacid = '{$_W['uniacid']}' and idcard like '%{$_GPC['idcard']}%' )";
    }

    if (!empty($dispatchtype) && $dispatchtype != 0 && $dispatchtype != -1) {
        $condition .= " AND  dispatchtype = '{$_SESSION['dispatchtype']}'";
        $condition2 .= " AND  dispatchtype = '{$_SESSION['dispatchtype']}'";
    }
    if ($dispatchtype == -1) {
        $condition .= " AND  selltype = '5' ";
        $condition2 .= " AND  selltype = '5'";
    }

    if ($_GPC['issued'] == -1) {

        $condition .= " AND  issued='0' and status='8'";
        $condition2 .= " AND  issued='0' and status='8'";
    }
    if ($_GPC['issued'] == 2) {

        $condition .= " AND  issued='2' ";
        $condition2 .= " AND  issued='2' ";
    }
    if ($_GPC['issued'] == 1) {

        $condition .= " AND  issued='1' ";
        $condition2 .= " AND  issued='1' ";
    }
    //message($_GPC['salers']);
    if ($godluck == -1) {
        $condition .= " AND  godluck='0' ";
        $condition2 .= " AND  godluck='0'";
    }
    if ($godluck == 1) {
        $condition .= " AND  godluck='1' ";
        $condition2 .= " AND  godluck='1'";
    }
    if (!empty($_GPC['addresstype'])) {
        $condition .= " AND  addresstype='{$_GPC['addresstype']}' ";
        $condition2 .= " AND  addresstype='{$_GPC['addresstype']}' ";
    }
    if (!empty($_GPC['salers'])) {
//        $condition .= " AND  veropenid='{$_GPC['salers']}' ";
//        $condition2 .= " AND  veropenid='{$_GPC['salers']}' ";
        $condition .= " AND  orderno in ( select orderno from " . tablename('tg_hexiao') . " where check_openid = '{$_GPC['salers']}') ";
        $condition2 .= " AND  orderno in ( select orderno from " . tablename('tg_hexiao') . " where check_openid = '{$_GPC['salers']}') ";
    }
    if ($status != '') {
        if ($status == 1) {
            $condition .= " AND status = '" . intval($status) . "' ";

        } else if ($status == 8) {
            $condition .= " AND status='8' ";

        } else if ($status == 4) {
            $condition .= " AND status in (4,7)";

        } else if ($status == 7) {
            $condition .= " AND status in (4,5,6,7)";

        } else if ($status == 17) {
            $condition .= " AND is_tuan='0'";
            $table = 'tg_order';
        } else {
            $condition .= " AND status = '" . intval($status) . "'";

        }

    }

    $sql = "select  * from " . tablename('tg_order') . " where $condition    ORDER BY createtime DESC " . "LIMIT " . ($pindex - 1) * $psize . ',' . $psize;
    $list = pdo_fetchall($sql);

    $paytype = array('0' => array('css' => 'default', 'name' => '未支付'), '1' => array('css' => 'info', 'name' => '余额支付'), '2' => array('css' => 'success', 'name' => '在线支付'), '3' => array('css' => 'warning', 'name' => '货到付款'));
    $orderstatus = array(
        '0' => array('css' => 'default', 'name' => '待付款'),
        '1' => array('css' => 'info', 'name' => '已付款'),
        '2' => array('css' => 'warning', 'name' => '待收货'),
        '3' => array('css' => 'success', 'name' => '已完成'),
        '4' => array('css' => 'success', 'name' => '已退款'),
        '5' => array('css' => 'success', 'name' => '强退款'),
        '6' => array('css' => 'danger', 'name' => '部分退款'),
        '7' => array('css' => 'success', 'name' => '已退款'),
        '8' => array('css' => 'success', 'name' => '待使用'),
        '9' => array('css' => 'success', 'name' => '已取消'),
        '10' => array('css' => 'success', 'name' => '待退款')
    );

    foreach ($list as $key => $value) {
        $options = pdo_fetch("select title,productprice,marketprice from " . tablename("tg_goods_option") . " where id=:id limit 1", array(":id" => $value['optionid']));
        //$list[$key]['optionname'] = $options['title'];
        $s = $value['status'];
        $list[$key]['statuscss'] = $orderstatus[$value['status']]['css'];
        $list[$key]['status'] = $orderstatus[$value['status']]['name'];
        $list[$key]['css'] = $paytype[$value['pay_type']]['css'];
        $list[$key]['resultfee'] = 0;
        if ($value['pay_type'] == 2) {
            if (empty($value['transid'])) {
                $list[$key]['paytype'] = '微信支付';
            } else {
                $list[$key]['paytype'] = '微信支付';
                /* $rfee = pdo_fetchall("SELECT * FROM " . tablename('tg_refund_record') . " WHERE transid = :id and status=1 and refund_id<>'' group by refund_id", array(':id' => $value['transid']));
                 $resultfee = 0;

                 foreach ($rfee as $rf => $rforder) {
                     $resultfee += $rforder['refundfee'];
                 }

                 $list[$key]['resultfee'] = $resultfee;*/
            }
        } else {
            $list[$key]['paytype'] = $paytype[$value['pay_type']]['name'];
        }
        $goodsss = pdo_fetch("select id,gname,gimg,merchantid,unit from" . tablename('tg_goods') . "where id = '{$value['g_id']}'");
        $collect = pdo_fetch("select active_times,province,city,county,detailed_address from " . tablename('tg_collect') . " where orderno = '{$value['orderno']}'");
        $list[$key]['active_times'] = $collect['active_times'];
        $list[$key]['unit'] = $goodsss['unit'];
        $list[$key]['gid'] = $goodsss['id'];
        $list[$key]['gname'] = $value['goodsname'];
        $list[$key]['gimg'] = $goodsss['gimg'];
        $list[$key]['merchant'] = pdo_fetch("select name from" . tablename('tg_merchant') . "where id = '{$value['merchantid']}' and uniacid={$_W['uniacid']}");
        $list[$key]['hexiao'] = pdo_fetchcolumn("select count(id) from " . tablename('tg_hexiao') . " where uniacid = '{$_W['uniacid']}' and orderno = '{$value['orderno']}' ");
        if ($value['addressid'] == 0 && $_W['uniacid'] == 5) {
            $list[$key]['address'] = $collect['province'].$collect['city'].$collect['county'].$collect['detailed_address'];
        }
    }


    //$alldan = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE dispatchtype='{$dispatchtype}' and is_tuan=0 and  uniacid='{$_W['uniacid']}' and mobile<>'虚拟'  ");

    //
    if ($dispatchtype > 0) {
        $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_order') . " WHERE $condition and mobile<>'虚拟' ", $paras);
        $pager = pagination($total, $pindex, $psize);
        $all = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and  uniacid='{$_W['uniacid']}' and mobile<>'虚拟'  ");
        $status0 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=0 and mobile<>'虚拟' ");//待付款
        $status1 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=1 and mobile<>'虚拟'  ");//已付款
        $status2 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=2 and mobile<>'虚拟' ");//待收货
        $status3 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=3 and mobile<>'虚拟' ");//已完成
        $status4 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status in (4,5,6,7) and mobile<>'虚拟' ");//已退款
        $status5 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=5 and mobile<>'虚拟' ");//强退款
        $status6 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=10 and mobile<>'虚拟' ");//部分退款
        $status7 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=7 and mobile<>'虚拟' ");//团长免单
        $status8 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=8 and mobile<>'虚拟' ");//待使用
        $status9 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=9 and mobile<>'虚拟' ");//已取消
        $status10 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and status=10 and mobile<>'虚拟' ");//已关闭
        $status17 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2 and dispatchtype='{$dispatchtype}' and uniacid='{$_W['uniacid']}' and is_tuan=0");//单买单

    } else {
        $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_order') . " WHERE $condition  and mobile<>'虚拟' ", $paras);
        $pager = pagination($total, $pindex, $psize);
        $all = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and mobile<>'虚拟'");
        $status0 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=0 and mobile<>'虚拟' ");//待付款
        $status1 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=1 and mobile<>'虚拟' ");//已付款
        $status2 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=2 and mobile<>'虚拟' ");//待收货
        $status3 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=3 and mobile<>'虚拟' ");//已完成
        $status4 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status in (4,5,6,7) and mobile<>'虚拟' ");//已退款
        $status5 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=5 and mobile<>'虚拟' ");//强退款
        $status6 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=10 and mobile<>'虚拟' ");//部分退款
        $status7 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=7 and mobile<>'虚拟' ");//团长免单
        $status8 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=8 and mobile<>'虚拟' ");//待使用
        $status9 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=9 and mobile<>'虚拟' ");//已取消
        $status10 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and status=10 and mobile<>'虚拟' ");//已关闭
        $status17 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE $condition2  and uniacid='{$_W['uniacid']}' and is_tuan=0");//单买单
        $status18 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE  status=8  and uniacid='{$_W['uniacid']}' and issued = 0 and selltype=5");//单买单
        $status19 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE status=8  and uniacid='{$_W['uniacid']}' and godluck = 1 and selltype=5");//单买单
        $status20 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE status=7  and uniacid='{$_W['uniacid']}' and godluck = 0 and selltype=5");//单买单
        $status21 = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_order') . " WHERE status=3 and uniacid='{$_W['uniacid']}' and issued = 2 and selltype=5");//单买单

    }

}

if ($op == 'detail') {
    wl_load()->model('activity');
    wl_load()->model('goods');
    load()->model('mc');
    $id = intval($_GPC['id']);
    $item = pdo_fetch("SELECT * FROM " . tablename('tg_order') . " WHERE id = :id", array(':id' => $id));
    $resultfee = 0;
    if (!empty($item['transid']) && $item['transid'] != '订单免费') {
        $rfee = pdo_fetchall("SELECT * FROM " . tablename('tg_refund_record') . " WHERE transid = :id and status=1 and refund_id<>'' group by refund_id", array(':id' => $item['transid']));
        foreach ($rfee as $rf => $rforder) {
            $resultfee += $rforder['refundfee'];
        }
    }

    $mfee = $item['price'] - $resultfee;
    $coupon_template = coupon_template($item['couponid']);
    $option = pdo_fetch("select title,productprice,marketprice from " . tablename("tg_goods_option") . " where id=:id limit 1", array(":id" => $item['optionid']));
    if ($item['status'] == 7) {
        $refund_record = pdo_fetch("SELECT * FROM " . tablename('tg_refund_record') . " WHERE orderid = :id", array(':id' => $id));
    }
    if (empty($item)) {
        message("抱歉，订单不存在!", referer(), "error");
    }
    $saler = pdo_fetch("select nickname from" . tablename('tg_member') . " where from_user ='" . $item['veropenid'] . "'");
    $uid = mc_openid2uid($item['openid']);
    $result = mc_fansinfo($uid, $_W['acid'], $_W['uniacid']);
    $item['fanid'] = $result['fanid'];
    $item['user'] = pdo_fetch("SELECT * FROM " . tablename('tg_address') . " WHERE id = {$item['addressid']}");

    if ($item['status'] == 4 || $item['status'] == 6 || $item['status'] == 7 || $item['status'] == 5) {
        $refund = pdo_fetch("select createtime from" . tablename('tg_refund_record') . "where orderid={$item['id']}");
        $refund_time = $refund['createtime'];
    }


    $store = pdo_fetch("select * from" . tablename('tg_store') . " where id ='" . $item['comadd'] . "'");
    // if (!empty($_GPC['notice'])) {
    //     pdo_update('tg_order', array('notice' => $_GPC['notice']), array('id' => $id));
    //     message('教师评语成功！', web_url('order/order/received'), 'success');
    // }
    include wl_template('order/order_detail');
    exit;
}
if ($op == 'judgment') {
    createJudgment($_GPC['jid'], $_GPC['content'], $_GPC['img']);
    message('评语成功！', referer(), 'success');
}
if ($op == 'confrimpay') {
    $id = $_GPC['id'];
    $item = pdo_fetch("SELECT * FROM " . tablename('tg_order') . " WHERE id = :id", array(':id' => $id));
    pdo_update('tg_order', array('status' => 1, 'pay_type' => 2, 'ptime' => TIMESTAMP), array('id' => $id));
    $oplogdata = serialize($item);
    oplog('admin', "后台确认付款", web_url('order/order/confrimpay'), $oplogdata);
    message('确认订单付款操作成功！', referer(), 'success');
}
if ($op == 'confirmsend') {
    $id = $_GPC['id'];

    $item = pdo_fetch("SELECT * FROM " . tablename('tg_order') . " WHERE id = :id", array(':id' => $id));
    $r = pdo_update('tg_order', array('status' => 2, 'express' => $_GPC['express'], 'expresssn' => $_GPC['expresssn'], 'sendtime' => TIMESTAMP), array('id' => $id));
    if ($r) {
        /*更新可结算金额*/
        if (!empty($item['merchantid'])) {
            merchant_update_no_money($item['price'], $item['merchantid']);
        }
        /*记录操作*/
        $oplogdata = serialize($item);
        oplog('admin', "后台确认发货", web_url('order/order/confirmsend'), $oplogdata);
        /*发货成功消息提醒*/
        $url = app_url('order/order/detail', array('id' => $item['id']));
        send_success($item['orderno'], $item['openid'], $_GPC['express'], $_GPC['expresssn'], $url);
        message('发货操作成功！', referer(), 'success');
    } else {
        message('发货操作失败！', referer(), 'error');
    }

}
if ($op == 'cancelsend') {
    $id = $_GPC['id'];
    $item = pdo_fetch("SELECT * FROM " . tablename('tg_order') . " WHERE id = :id", array(':id' => $id));
    $r = pdo_update('tg_order', array('status' => 8, 'express' => '', 'expresssn' => '', 'sendtime' => ''), array('id' => $id));
    if ($r) {
        /*更新可结算金额*/
        if (!empty($item['merchantid'])) {
            merchant_update_no_money(0 - $item['price'], $item['merchantid']);
        }
        /*记录操作*/
        $oplogdata = serialize($item);
        oplog('admin', "后台取消发货", web_url('order/order/cancelsend'), $oplogdata);
        message('取消发货操作成功！', referer(), 'success');
    } else {
        message('取消发货操作失败！', referer(), 'error');
    }

}
if ($op == 'refund') {
    $id = $_GPC['id'];
    $item = pdo_fetch("SELECT * FROM " . tablename('tg_order') . " WHERE id = :id", array(':id' => $id));
    $orderno = $item['orderno'];
    if ($_GPC['refundall'] == 1) {
        $res = refund($orderno, 2);
    } else {
        $rfee = pdo_fetchall("SELECT * FROM " . tablename('tg_refund_record') . " WHERE transid = :id and status=1 ", array(':id' => $item['transid']));
        $resultfee = 0;
        if (!empty($item['transid'])) {
            foreach ($rfee as $rf => $rforder) {
                $resultfee += $rforder['refundfee'];
            }
        }
        $mfee = $item['price'] - $resultfee;
        $res = partrefund($orderno, 2, $_GPC['refundnum']);
    }

    if ($res == 'success') {
        $oplogdata = serialize($item);
        oplog('admin', "后台订单详情退款", web_url('order/order/refund'), $oplogdata);
        /*退款成功消息提醒*/
        $url = app_url('order/order/detail', array('id' => $item['id']));
        $goods = pdo_fetch("select * from" . tablename('tg_goods') . "where id='{$item['g_id']}'");
        refund_success($item['orderno'], $goods['gname'], $item['openid'], $item['price'], time(), $url);

        message('退款成功了！', referer(), 'success');
    } else {
        message('退款失败，服务器正忙，请稍等等！', referer(), 'fail');
    }
}
if ($op == 'import') {
    $file = $_FILES['fileName'];
    $max_size = "2000000";
    $fname = $file['name'];
    $ftype = strtolower(substr(strrchr($fname, '.'), 1));
    //文件格式
    $uploadfile = $file['tmp_name'];
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (is_uploaded_file($uploadfile)) {
            if ($file['size'] > $max_size) {
                echo "Import file is too large";
                exit;
            }
            if ($ftype == 'xls') {
                require_once '../framework/library/phpexcel/PHPExcel.php';
                $objReader = PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($uploadfile);
                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $succ_result = 0;
                $error_result = 0;
                for ($j = 2; $j <= $highestRow; $j++) {
                    $orderNo = trim($objPHPExcel->getActiveSheet()->getCell("A$j")->getValue());
                    $expressOrder = trim($objPHPExcel->getActiveSheet()->getCell("AB$j")->getValue());

                    if (!empty($expressOrder)) {
                        $order = pdo_fetch("select * from" . tablename('tg_order') . "where orderno ='{$orderNo}'");
                        if ($expressOrder == '是') {
                            $godluck = 1;
                            $cj_message = 2;
                        } else {
                            $godluck = 0;
                            $cj_message = 1;
                        }
                        $res = pdo_update('tg_order', array('godluck' => $godluck, 'issued' => 1, 'cj_message' => $cj_message), array('orderno' => $orderNo, 'selltype' => 5));
                        if ($res) {
                            /*更新可结算金额*/
                            if (!empty($order['merchantid'])) {
                                merchant_update_no_money($order['price'], $order['merchantid']);
                            }
                            /*记录操作*/
                            $oplogdata = serialize($item);
                            oplog('admin', "后台批量发货", web_url('order/import/import'), $oplogdata);
                            $url = app_url('order/order/detail', array('id' => $order['id']));
                            //send_success($order['orderno'], $order['openid'], $expressName, $orderNo, $url);
                            $succ_result += 1;
                        } else {
                            $error_result += 1;
                        }
                    } else {
                        if (!empty($orderNo)) {
                            $error_result += 1;
                        }
                    }
                }
            } elseif ($ftype == 'csv') {
                if (empty ($uploadfile)) {
                    echo '请选择要导入的CSV文件！';
                    exit;
                }
                $handle = fopen($uploadfile, 'r');
                $n = 0;
                while ($data = fgetcsv($handle, 10000)) {
                    $num = count($data);
                    for ($i = 0; $i < $num; $i++) {
                        $out[$n][$i] = $data[$i];
                    }
                    $n++;
                }
                $result = $out; //解析csv
                $len_result = count($result);
                if ($len_result == 0) {
                    echo '没有任何数据！';
                    exit;
                }
                $succ_result = 0;
                $error_result = 0;
                for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值
                    $orderNo = trim(iconv('gb2312', 'utf-8', $result[$i][0])); //中文转码
                    if ($orderNo == '') {
                        continue;
                    }

                    $expressOrder = trim(iconv('gb2312', 'utf-8', $result[$i][27]));

                    if (!empty($expressOrder)) {

                        $order = pdo_fetch("select * from" . tablename('tg_order') . "where orderno ='{$orderNo}'");
                        if ($expressOrder == '是') {
                            $godluck = 1;
                            $cj_message = 2;
                        } else {
                            $godluck = 0;
                            $cj_message = 1;
                        }
                        $res = pdo_update('tg_order', array('godluck' => $godluck, 'issued' => 1, 'cj_message' => $cj_message), array('orderno' => $orderNo, 'selltype' => 5));
                        if ($res) {
                            $url = app_url('order/order/detail', array('id' => $order['id']));
                            //send_success($order['orderno'], $order['openid'], $expressName, $orderNo, $url);
                            $succ_result += 1;
                        } else {
                            $error_result += 1;
                        }
                    } else {
                        $error_result += 1;
                    }
                }
                fclose($handle); //关闭指针
            } else {
                echo "文件后缀格式必须为xls或csv";
                exit;
            }
        } else {
            echo "文件名不能为空!";
            exit;
        }
    }
    message('导入抽奖订单操作成功！成功' . $succ_result . '条，失败' . $error_result . '条', referer(), 'success');
}
if ($op == 'output') {
    set_time_limit(0);
    $condition = "  uniacid = :uniacid";
    $paras = array(':uniacid' => $_W['uniacid']);
    $status = $_GPC['status'];
    $transid = $_GPC['transid'];
    $pay_type = $_GPC['pay_type'];
    $keyword = $_GPC['keyword'];
    $member = $_GPC['member'];
    $time = $_GPC['time'];
    $idcard = $_GPC['idcard'];

    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime = time();
    }
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);
        $condition .= " AND  createtime >= :starttime AND  createtime <= :endtime ";
        $paras[':starttime'] = $starttime;
        $paras[':endtime'] = $endtime;
    }
    if (trim($_GPC['goodsid']) != '') {
        $condition .= " and (g_id = '{$_GPC['goodsid']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid']}')) ";
        $condition2 .= " and g_id like '%{$_GPC['goodsid']}%' ";;
    }
    if (trim($_GPC['goodsid2']) != '') {
        $condition .= " and (g_id = '{$_GPC['goodsid2']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid2']}')) ";
        $condition2 .= " and (g_id = '{$_GPC['goodsid2']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid2']}')) ";

        //$condition .= " and g_id like '%{$_GPC['goodsid2']}%' ";
        //$condition2 .= " and g_id like '%{$_GPC['goodsid2']}%' ";
    }
    if (trim($_GPC['address']) != '') {

        $condition .= " and address like '%{$_GPC['address']}%' ";
    }
    if (!empty($_GPC['merchantid'])) {
        $condition .= " AND  merchantid={$_GPC['merchantid']} ";
    }
    if (!empty($_GPC['transid'])) {
        $condition .= " AND  transid LIKE  '%{$_GPC['transid']}%'";
    }
    if (!empty($_GPC['addresstype'])) {
        $condition .= " AND  addresstype={$_GPC['addresstype']} ";
    }
    if (!empty($_GPC['pay_type'])) {
        $condition .= " AND  pay_type = '{$_GPC['pay_type']}'";
    } elseif ($_GPC['pay_type'] === '0') {
        $condition .= " AND  pay_type = '{$_GPC['pay_type']}'";
    }
    if (!empty($_GPC['keyword'])) {
        $condition .= " AND  orderno LIKE '%{$_GPC['keyword']}%'";
    }
    if (!empty($_GPC['salers'])) {
//        $condition .= " AND  veropenid='{$_GPC['salers']}' ";
//        $condition2 .= " AND  veropenid='{$_GPC['salers']}' ";
        $condition .= " AND  orderno in ( select orderno from " . tablename('tg_hexiao') . " where check_openid = '{$_GPC['salers']}') ";
        $condition2 .= " AND  orderno in ( select orderno from " . tablename('tg_hexiao') . " where check_openid = '{$_GPC['salers']}') ";
    }
    if (!empty($_GPC['member'])) {
        $condition .= " AND (addname LIKE '%{$_GPC['member']}%')";
    }
    if (!empty($_GPC['mobile'])) {
        $condition .= " AND ( mobile = '{$_GPC['mobile']}')";
    }

    if (!empty($_GPC['idcard'])) {
        $condition .= " and openid in ( select from_user from " . tablename('tg_member') . " where idcard like '%{$_GPC['idcard']}%' )";
        $condition2 .= " and openid in ( select from_user from " . tablename('tg_member') . " where idcard like '%{$_GPC['idcard']}%' )";
    }

    if (!empty($dispatchtype) && $dispatchtype != 0 && $dispatchtype != -1) {
        $condition .= " AND  dispatchtype = '{$_SESSION['dispatchtype']}'";
    }
    if ($dispatchtype == -1) {
        $condition .= " AND  selltype = 5";
    }
    if ($_GPC['issued'] == -1) {

        $condition .= " AND  issued=0 and status=8 ";
        $condition2 .= " AND  issued=0 and status=8";
    }
    if ($_GPC['issued'] == 2) {

        $condition .= " AND  issued=2 ";
        $condition2 .= " AND  issued=2 ";
    }
    if ($_GPC['issued'] == 1) {

        $condition .= " AND  issued=1 ";
        $condition2 .= " AND  issued=1 ";
    }
    if ($status != '') {
        if ($status == 1) {
            $condition .= " AND status = '" . intval($status) . "' ";

        } else if ($status == 8) {
            $condition .= " AND status=8 ";

        } else if ($status == 4) {
            $condition .= " AND status in (4,7)";
            $condition .= " AND is_tuan=1";
        } else if ($status == 7) {
            $condition .= " AND status in (4,5,6,7)";

        } else if ($status == 17) {
            $condition .= " AND is_tuan=0";
            $table = 'tg_order';
        } else {
            $condition .= " AND status = '" . intval($status) . "'";

        }

    }
    $condition .= " and master_orderno is NULL";
    //message($condition);
    $sql = "select * from " . tablename('tg_order') . " where $condition  order by successtime asc";
    $orders = pdo_fetchall($sql, $paras);
    //$orders = pdo_fetchall("select * from" . tablename('tg_order') . "where $condition and mobile<>'虚拟' order by successtime asc");
    switch ($status) {
        case NULL:
            $str = '全部订单_' . time();
            break;
        case 1:
            $str = '已支付订单_' . time();
            break;
        case 8:
            $str = '待使用订单' . time();
            break;
        case 2:
            $str = '待收货订单' . time();
            break;
        case 3:
            $str = '已完成订单' . time();
            break;
        case 4:
            $str = '已退款订单' . time();
            break;
        case 5:
            $str = '强退款订单' . time();
            break;
        case 6:
            $str = '部分退款订单' . time();
            break;
        case 7:
            $str = '已退款订单' . time();
            break;
        case 9:
            $str = '已取消订单' . time();
            break;
        case 10:
            $str = '待退款订单' . time();
            break;
        default:
            $str = '待支付订单' . time();
            break;
    }
    /* 输入到CSV文件 */
    $html = "\xEF\xBB\xBF";
    /* 输出表头 */
    $filter = array('aa' => '订单编号', 'bb' => '姓名', 'cc' => '电话', 'dd' => '总价(元)', 'ee' => '状态', 'ff' => '下单时间', 'gg' => '项目名称', 'm3' => '核销员', 'm4' => '核销门店', 'm8' => '核销时间');
    foreach ($filter as $key => $title) {
        $html .= $title . "\t,";
    }
    $html .= "\n";
    foreach ($orders as $k => $v) {
        if ($v['status'] == '0') {
            $thistatus = '未支付';
        }
        if ($v['status'] == '1') {
            $thistatus = '已支付';
        }
        if ($v['status'] == '2') {
            $thistatus = '待收货';
        }
        if ($v['status'] == '3') {
            $thistatus = '已完成';
        }
        if ($v['status'] == '4') {
            $thistatus = '已退款';
        }
        if ($v['status'] == '5') {
            $thistatus = '强退款';
        }
        if ($v['status'] == '6') {
            $thistatus = '部分退款';
        }
        if ($v['status'] == '7') {
            $thistatus = '已退款';
        }
        if ($v['status'] == '8') {
            $thistatus = '待使用';
        }
        if ($v['status'] == '9') {
            $thistatus = '已取消';
        }
        if ($v['status'] == '10') {
            $thistatus = '待退款';
        }
        if ($v['status'] == '') {
            $thistatus = '全部订单';
        }
        if ($v['dispatchtype'] == 0) {
            $disname = '无';
        }
        if ($v['dispatchtype'] == 1) {
            $disname = '送货上门';
        }
        if ($v['dispatchtype'] == 2) {
            $disname = '快递';
        }
        if ($v['dispatchtype'] == 3) {
            $disname = '自提';
            $v['address'] = '';
        }
        wl_load()->model('member');
        $mermber = member_get_by_params(" openid='{$v['openid']}' ");
        if (!empty($v['addressid']) && $v['addressid'] > 0 && $v['dispatchtype'] != 3) {
            $add = pdo_fetch("select * from" . tablename('tg_address') . "where id = '{$v['addressid']}' ");
        }

        $goods = pdo_fetch("select * from" . tablename('tg_goods') . "where id = '{$v['g_id']}' and uniacid='{$_W['uniacid']}'");
        if (empty($v['createtime'])) {
            $v['createtime'] = 0;
        }
        $time = date('Y-m-d H:i:s', $v['createtime']);
        if ($v['addresstype'] == 1) {
            $addresstype = '公司';
        } else {
            $addresstype = '家庭';
        }
        if (!empty($v['veropenid'])) {
            $saler = pdo_fetch("select nickname from" . tablename('tg_member') . " where from_user ='" . $v['veropenid'] . "'");
        }
        if (!empty($v['checkstore'])) {
            $realstore = pdo_fetch("select storename from" . tablename('tg_store') . " where id ='" . $v['checkstore'] . "'");
        }

        if (!empty($v['comadd']) && $v['dispatchtype'] == 3) {
            $com = pdo_fetch("select storename from" . tablename('tg_store') . " where id ='" . $v['comadd'] . "'");
        }
        if (!empty($v['hexiaotime'])) {
            $hexiaotime = date('Y-m-d H:i:s', $v['hexiaotime']);

        }
        $options = pdo_fetch("select title,productprice,marketprice from " . tablename("tg_goods_option") . " where id=:id limit 1", array(":id" => $v['optionid']));
        if (empty($options['title'])) {
            $options['title'] = '不限';
        }
        $resultfee = 0;
        $rfee = pdo_fetchall("SELECT refundfee FROM " . tablename('tg_refund_record') . " WHERE transid = :id and status=1 and uniacid=:uniacid ", array(':id' => $v['transid'], ':uniacid' => $_W['uniacid']));
        foreach ($rfee as $rf => $rforder) {
            $resultfee += $rforder['refundfee'];
        }

        if ($v['g_id'] > 0) {
            $gname = $v['goodsname'];
            $gopname = $v['optionname'];
            $orders[$k]['aa'] = $v['orderno'];
//				$orders[$k]['ll'] = $v['tuan_id'];
            //$orders[$k]['qq'] = $mermber['nickname'];
            $orders[$k]['bb'] = $v['addname'];
            $orders[$k]['cc'] = $v['mobile'];
//			$orders[$k]['m2'] = $v['freight'];
            $orders[$k]['dd'] = $v['pay_price'];
//			$orders[$k]['d1'] = $resultfee;
            $orders[$k]['ee'] = $thistatus;
            $orders[$k]['ff'] = $time;
            $orders[$k]['gg'] = $gname;
//			$orders[$k]['h1'] = $add['province'];
//			$orders[$k]['h2'] = $add['city'];
//			$orders[$k]['h3'] = $add['county'];
//			$orders[$k]['h4'] = $add['detailed_address'];
//			$orders[$k]['hh'] = $v['address'];
//			$orders[$k]['ii'] = $v['transid'];
//			$orders[$k]['jj'] = $v['expresssn'];
//			$orders[$k]['kk'] = $v['express'];
//			$orders[$k]['m1'] = $disname;


//			$orders[$k]['mz'] = date('Y-m-d H:i:s', $v['successtime']);
//			$orders[$k]['mm'] = $v['remark'];
            $orders[$k]['m3'] = $saler['nickname'];
            $orders[$k]['m4'] = $realstore['storename'];
//			$orders[$k]['m5'] = $com['storename'];
//			$orders[$k]['m6'] = $addresstype;
//			$orders[$k]['m7'] = '';
//			$orders[$k]['m9'] = $v['adminremark'];
            $orders[$k]['m8'] = date('Y-m-d H:i:s', $v['hexiaotime']);
//			$orders[$k]['oo'] = $v['gnum'];
//			$orders[$k]['pp'] = $gopname;

            foreach ($filter as $key => $title) {
                $html .= $orders[$k][$key] . "\t,";
            }
            $html .= "\n";
        } else {
            $col = pdo_fetchall("select * from " . tablename('tg_collect') . " where  orderno=:orderno", array('orderno' => $v['orderno']));
            $orders[$k]['aa'] = $v['orderno'];
            $orders[$k]['bb'] = $v['addname'];
            $orders[$k]['cc'] = $v['mobile'];
            $orders[$k]['dd'] = $v['price'];
//			$orders[$k]['d1'] = $resultfee;
            $orders[$k]['ee'] = $thistatus;
            $orders[$k]['ff'] = $time;
            $orders[$k]['gg'] = '';
//			$orders[$k]['h1'] = $add['province'];
//			$orders[$k]['h2'] = $add['city'];
//			$orders[$k]['h3'] = $add['county'];
//			$orders[$k]['h4'] = $add['detailed_address'];
//			$orders[$k]['hh'] = $v['address'];
//			$orders[$k]['ii'] = $v['transid'];
//			$orders[$k]['jj'] = $v['expresssn'];
//			$orders[$k]['kk'] = $v['express'];
//			$orders[$k]['m1'] = $disname;
//			$orders[$k]['m2'] = $v['freight'];
            $orders[$k]['m3'] = $saler['nickname'];
            $orders[$k]['m4'] = $realstore['storename'];
//			$orders[$k]['m5'] = $com['storename'];
//			$orders[$k]['m6'] = $addresstype;
            $orders[$k]['m8'] = date('Y-m-d H:i:s', $v['hexiaotime']);
//			$orders[$k]['m9'] = $v['adminremark'];
//			$orders[$k]['ll'] = $v['tuan_id'];
            if (empty($v['successtime'])) {
                $v['successtime'] = time();
            }
//			$orders[$k]['mz'] = date('Y-m-d H:i:s', $v['successtime']);
//			$orders[$k]['mm'] = $v['remark'];
//			$orders[$k]['oo'] = '';
//			$orders[$k]['pp'] = '';

            foreach ($filter as $key => $title) {
                $html .= $orders[$k][$key] . "\t,";
            }
            $html .= "\n";
            foreach ($col as $c => $cc) {
                $gname = $cc['goodsname'];
                $gopname = $cc['item'];
                $orders[$k]['aa'] = '';
                $orders[$k]['bb'] = '';
                $orders[$k]['cc'] = '';
                $orders[$k]['dd'] = $cc['oprice'];
                $orders[$k]['ee'] = '';
                $orders[$k]['ff'] = $time;
                $orders[$k]['gg'] = $gname;
//					$orders[$k]['h1'] = '';
//					$orders[$k]['h2'] = '';
//					$orders[$k]['h3'] = '';
//					$orders[$k]['h4'] = '';
//					$orders[$k]['hh'] = '';
//					$orders[$k]['ii'] = '';
//					$orders[$k]['jj'] = '';
//					$orders[$k]['kk'] = '';
//					$orders[$k]['m1'] = '';
//					$orders[$k]['m2'] = '';
                $orders[$k]['m3'] = '';
                $orders[$k]['m4'] = '';
//					$orders[$k]['m5'] = '';
//					$orders[$k]['m6'] = $addresstype;
//					$orders[$k]['m7'] = '';
                $orders[$k]['m8'] = '';
//					$orders[$k]['m9'] = '';
//					$orders[$k]['ll'] = '';
//					$orders[$k]['mz'] = '';
//					$orders[$k]['mm'] = '';
//					$orders[$k]['oo'] = $cc['num'];
//					$orders[$k]['pp'] = $gopname;

                foreach ($filter as $key => $title) {
                    $html .= $orders[$k][$key] . "\t,";
                }
                $html .= "\n";
            }
        }
        unset($com);
        unset($saler);
        unset($add);
    }
    /* 输出CSV文件 */
    header("Content-type:text/csv");
    header("Content-Disposition:attachment; filename={$str}.csv");
    echo $html;
    exit();
}
if ($op == 'remark') {
    $orderid = intval($_GPC['id']);
    $remark = $_GPC['remark'];
    if (pdo_update('tg_order', array('adminremark' => $remark), array('id' => $orderid))) {
        die(json_encode(array('errno' => 0)));
    } else {
        die(json_encode(array('errno' => 1)));
    }
}
if ($op == 'address') {
    $orderid = intval($_GPC['id']);
    $address = $_GPC['address'];
    $realname = $_GPC['realname'];
    $mobile = $_GPC['mobile'];
    $item = pdo_fetch("SELECT * FROM " . tablename('tg_order') . " WHERE id = :id", array(':id' => $orderid));
    if (pdo_update('tg_order', array('address' => $address, 'addname' => $realname, 'mobile' => $mobile), array('id' => $orderid))) {
        $oplogdata = serialize($item);
        oplog('admin', "后台修改地址", web_url('order/order/confrimpay'), $oplogdata);
        die(json_encode(array('errno' => 0)));
    } else {
        die(json_encode(array('errno' => 1)));
    }
}
if ($op == 'master_refund') {
    $orderno = $_GPC['orderno'];
    pdo_update('tg_order', array('status' => 10), array('orderno' => $orderno));
    pdo_update('tg_order', array('status' => 10), array('master_orderno' => $orderno));
    die(json_encode(array('errno' => 1)));
}
if ($op == 'outputSNG') {
    set_time_limit(0);
    $condition = "  uniacid = :uniacid";
    $paras = array(':uniacid' => $_W['uniacid']);
    $status = $_GPC['status'];
    $transid = $_GPC['transid'];
    $pay_type = $_GPC['pay_type'];
    $keyword = $_GPC['keyword'];
    $member = $_GPC['member'];
    $time = $_GPC['time'];
    $idcard = $_GPC['idcard'];

    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime = time();
    }
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);
        $condition .= " AND  createtime >= :starttime AND  createtime <= :endtime ";
        $paras[':starttime'] = $starttime;
        $paras[':endtime'] = $endtime;
    }
    if (trim($_GPC['goodsid']) != '') {
        $condition .= " and (g_id = '{$_GPC['goodsid']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid']}')) ";
        $condition2 .= " and g_id like '%{$_GPC['goodsid']}%' ";;
    }
    if (trim($_GPC['goodsid2']) != '') {
        $condition .= " and (g_id = '{$_GPC['goodsid2']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid2']}')) ";
        $condition2 .= " and (g_id = '{$_GPC['goodsid2']}' or orderno in (select orderno from ims_tg_collect where sid ='{$_GPC['goodsid2']}')) ";

        //$condition .= " and g_id like '%{$_GPC['goodsid2']}%' ";
        //$condition2 .= " and g_id like '%{$_GPC['goodsid2']}%' ";
    }
    if (trim($_GPC['address']) != '') {

        $condition .= " and address like '%{$_GPC['address']}%' ";
    }
    if (!empty($_GPC['merchantid'])) {
        $condition .= " AND  merchantid={$_GPC['merchantid']} ";
    }
    if (!empty($_GPC['transid'])) {
        $condition .= " AND  transid LIKE  '%{$_GPC['transid']}%'";
    }
    if (!empty($_GPC['addresstype'])) {
        $condition .= " AND  addresstype={$_GPC['addresstype']} ";
    }
    if (!empty($_GPC['pay_type'])) {
        $condition .= " AND  pay_type = '{$_GPC['pay_type']}'";
    } elseif ($_GPC['pay_type'] === '0') {
        $condition .= " AND  pay_type = '{$_GPC['pay_type']}'";
    }
    if (!empty($_GPC['keyword'])) {
        $condition .= " AND  orderno LIKE '%{$_GPC['keyword']}%'";
    }
    if (!empty($_GPC['salers'])) {
//        $condition .= " AND  veropenid='{$_GPC['salers']}' ";
//        $condition2 .= " AND  veropenid='{$_GPC['salers']}' ";
        $condition .= " AND  orderno in ( select orderno from " . tablename('tg_hexiao') . " where check_openid = '{$_GPC['salers']}') ";
        $condition2 .= " AND  orderno in ( select orderno from " . tablename('tg_hexiao') . " where check_openid = '{$_GPC['salers']}') ";
    }
    if (!empty($_GPC['member'])) {
        $condition .= " AND (addname LIKE '%{$_GPC['member']}%')";
    }
    if (!empty($_GPC['mobile'])) {
        $condition .= " AND ( mobile = '{$_GPC['mobile']}')";
    }

    if (!empty($_GPC['idcard'])) {
        $condition .= " and openid in ( select from_user from " . tablename('tg_member') . " where idcard like '%{$_GPC['idcard']}%' )";
        $condition2 .= " and openid in ( select from_user from " . tablename('tg_member') . " where idcard like '%{$_GPC['idcard']}%' )";
    }

    if (!empty($dispatchtype) && $dispatchtype != 0 && $dispatchtype != -1) {
        $condition .= " AND  dispatchtype = '{$_SESSION['dispatchtype']}'";
    }
    if ($dispatchtype == -1) {
        $condition .= " AND  selltype = 5";
    }
    if ($_GPC['issued'] == -1) {

        $condition .= " AND  issued=0 and status=8 ";
        $condition2 .= " AND  issued=0 and status=8";
    }
    if ($_GPC['issued'] == 2) {

        $condition .= " AND  issued=2 ";
        $condition2 .= " AND  issued=2 ";
    }
    if ($_GPC['issued'] == 1) {

        $condition .= " AND  issued=1 ";
        $condition2 .= " AND  issued=1 ";
    }
    if ($status != '') {
        if ($status == 1) {
            $condition .= " AND status = '" . intval($status) . "' ";

        } else if ($status == 8) {
            $condition .= " AND status=8 ";

        } else if ($status == 4) {
            $condition .= " AND status in (4,7)";
            $condition .= " AND is_tuan=1";
        } else if ($status == 7) {
            $condition .= " AND status in (4,5,6,7)";

        } else if ($status == 17) {
            $condition .= " AND is_tuan=0";
            $table = 'tg_order';
        } else {
            $condition .= " AND status = '" . intval($status) . "'";

        }

    }
    $condition .= " and master_orderno is NULL";
    //message($condition);
    $sql = "select * from " . tablename('tg_order') . " where $condition and mobile<>'虚拟' order by successtime asc";
    $orders = pdo_fetchall($sql, $paras);
    //$orders = pdo_fetchall("select * from" . tablename('tg_order') . "where $condition and mobile<>'虚拟' order by successtime asc");
    switch ($status) {
        case NULL:
            $str = '全部订单_' . time();
            break;
        case 1:
            $str = '已支付订单_' . time();
            break;
        case 8:
            $str = '待使用订单' . time();
            break;
        case 2:
            $str = '待收货订单' . time();
            break;
        case 3:
            $str = '已完成订单' . time();
            break;
        case 4:
            $str = '已退款订单' . time();
            break;
        case 5:
            $str = '强退款订单' . time();
            break;
        case 6:
            $str = '部分退款订单' . time();
            break;
        case 7:
            $str = '已退款订单' . time();
            break;
        case 9:
            $str = '已取消订单' . time();
            break;
        case 10:
            $str = '待退款订单' . time();
            break;
        default:
            $str = '待支付订单' . time();
            break;
    }
    /* 输入到CSV文件 */
    $html = "\xEF\xBB\xBF";
    /* 输出表头 */
    $filter = array('aa' => '订单编号', 'bb' => '姓名', 'cc' => '手机号码', 'hh' => '身份证号', 'ii' => '性别', 'jj' => '年级', 'kk' => '预约场次', 'dd' => '总价(元)', 'ee' => '状态', 'ff' => '下单时间', 'gg' => '项目名称', 'm3' => '核销员', 'm4' => '核销门店', 'm8' => '核销时间');
    foreach ($filter as $key => $title) {
        $html .= $title . "\t,";
    }
    $html .= "\n";
    foreach ($orders as $k => $v) {
        if ($v['status'] == '0') {
            $thistatus = '未支付';
        }
        if ($v['status'] == '1') {
            $thistatus = '已支付';
        }
        if ($v['status'] == '2') {
            $thistatus = '待收货';
        }
        if ($v['status'] == '3') {
            $thistatus = '已完成';
        }
        if ($v['status'] == '4') {
            $thistatus = '已退款';
        }
        if ($v['status'] == '5') {
            $thistatus = '强退款';
        }
        if ($v['status'] == '6') {
            $thistatus = '部分退款';
        }
        if ($v['status'] == '7') {
            $thistatus = '已退款';
        }
        if ($v['status'] == '8') {
            $thistatus = '待使用';
        }
        if ($v['status'] == '9') {
            $thistatus = '已取消';
        }
        if ($v['status'] == '10') {
            $thistatus = '待退款';
        }
        if ($v['status'] == '') {
            $thistatus = '全部订单';
        }
        if ($v['dispatchtype'] == 0) {
            $disname = '无';
        }
        if ($v['dispatchtype'] == 1) {
            $disname = '送货上门';
        }
        if ($v['dispatchtype'] == 2) {
            $disname = '快递';
        }
        if ($v['dispatchtype'] == 3) {
            $disname = '自提';
            $v['address'] = '';
        }
        wl_load()->model('member');
        $mermber = member_get_by_params(" openid='{$v['openid']}' ");
        if (!empty($v['addressid']) && $v['addressid'] > 0 && $v['dispatchtype'] != 3) {
            $add = pdo_fetch("select * from" . tablename('tg_address') . "where id = '{$v['addressid']}' ");
        }

        $goods = pdo_fetch("select * from" . tablename('tg_goods') . "where id = '{$v['g_id']}' and uniacid='{$_W['uniacid']}'");
        if (empty($v['createtime'])) {
            $v['createtime'] = 0;
        }
        $time = date('Y-m-d H:i:s', $v['createtime']);
        if ($v['addresstype'] == 1) {
            $addresstype = '公司';
        } else {
            $addresstype = '家庭';
        }
        if (!empty($v['veropenid'])) {
            $saler = pdo_fetch("select nickname from" . tablename('tg_member') . " where from_user ='" . $v['veropenid'] . "'");
        }
        if (!empty($v['checkstore'])) {
            $realstore = pdo_fetch("select storename from" . tablename('tg_store') . " where id ='" . $v['checkstore'] . "'");
        }

        if (!empty($v['comadd']) && $v['dispatchtype'] == 3) {
            $com = pdo_fetch("select storename from" . tablename('tg_store') . " where id ='" . $v['comadd'] . "'");
        }
        if (!empty($v['hexiaotime'])) {
            $hexiaotime = date('Y-m-d H:i:s', $v['hexiaotime']);

        }
        $options = pdo_fetch("select title,productprice,marketprice from " . tablename("tg_goods_option") . " where id=:id limit 1", array(":id" => $v['optionid']));
        if (empty($options['title'])) {
            $options['title'] = '不限';
        }
        $resultfee = 0;
        $rfee = pdo_fetchall("SELECT refundfee FROM " . tablename('tg_refund_record') . " WHERE transid = :id and status=1 and uniacid=:uniacid ", array(':id' => $v['transid'], ':uniacid' => $_W['uniacid']));
        foreach ($rfee as $rf => $rforder) {
            $resultfee += $rforder['refundfee'];
        }
//
//        if ($v['g_id'] > 0) {
//            $gname = $v['goodsname'];
//            $gopname = $v['optionname'];
//            $orders[$k]['aa'] = $v['orderno'];
////				$orders[$k]['ll'] = $v['tuan_id'];
//            //$orders[$k]['qq'] = $mermber['nickname'];
//            $orders[$k]['bb'] = $v['addname'];
//            $orders[$k]['cc'] = $v['mobile'];
//            $orders[$k]['hh'] = $v['card_fee'];
//            $orders[$k]['ii'] = $v['sex'];
//            $orders[$k]['jj'] = $v['class_num'];
//            if(!empty($v['starttime'])) {
//                $orders[$k]['kk'] = date('Y-m-d H:i:s', $v['starttime']);
//            }
////			$orders[$k]['m2'] = $v['freight'];
//            $orders[$k]['dd'] = doubleval($v['pay_price']);
////			$orders[$k]['d1'] = $resultfee;
//            $orders[$k]['ee'] = $thistatus;
//            $orders[$k]['ff'] = $time;
//            $orders[$k]['gg'] = $gname;
////			$orders[$k]['h1'] = $add['province'];
////			$orders[$k]['h2'] = $add['city'];
////			$orders[$k]['h3'] = $add['county'];
////			$orders[$k]['h4'] = $add['detailed_address'];
////			$orders[$k]['hh'] = $v['address'];
////			$orders[$k]['ii'] = $v['transid'];
////			$orders[$k]['jj'] = $v['expresssn'];
////			$orders[$k]['kk'] = $v['express'];
////			$orders[$k]['m1'] = $disname;
//
//
////			$orders[$k]['mz'] = date('Y-m-d H:i:s', $v['successtime']);
////			$orders[$k]['mm'] = $v['remark'];
//            $orders[$k]['m3'] = $saler['nickname'];
//            $orders[$k]['m4'] = $realstore['storename'];
////			$orders[$k]['m5'] = $com['storename'];
////			$orders[$k]['m6'] = $addresstype;
////			$orders[$k]['m7'] = '';
////			$orders[$k]['m9'] = $v['adminremark'];
////            $orders[$k]['m8'] = date('Y-m-d H:i:s', $v['hexiaotime']);
//            if(!empty($v['hexiaotime'])) {
//                $orders[$k]['m8'] = date('Y-m-d H:i:s', $v['hexiaotime']);
//            }else{
//                $orders[$k]['m8'] = '';
//            }
////			$orders[$k]['oo'] = $v['gnum'];
////			$orders[$k]['pp'] = $gopname;
//
//            foreach ($filter as $key => $title) {
//                $html .= $orders[$k][$key] . "\t,";
//            }
//            $html .= "\n";
//        } else {
        $col = pdo_fetchall("select * from " . tablename('tg_collect') . " where  orderno=:orderno", array('orderno' => $v['orderno']));
        $orders[$k]['aa'] = $v['orderno'];
        $orders[$k]['bb'] = $v['addname'];
        $orders[$k]['cc'] = $v['mobile'];
        $orders[$k]['hh'] = $v['card_fee'];
        $orders[$k]['ii'] = $v['sex'];
        $collect = pdo_fetch("select * from " . tablename('tg_collect') . " where orderno = '" . $v['orderno'] . "' ");
        $member = pdo_fetch("select garde from " . tablename('tg_member') . " where idcard = '" . $v['card_fee'] . "' ");
//        file_put_contents(TG_DATA."data.log", var_export(array('data' => $member , 'time' => $time), true).PHP_EOL, FILE_APPEND);
        $orders[$k]['jj'] = $member['garde'];
        if (!empty($collect['starttime'])) {
            $orders[$k]['kk'] = date('Y-m-d H:i:s', $collect['starttime']);
        }
        $orders[$k]['dd'] = doubleval($v['price']);
//			$orders[$k]['d1'] = $resultfee;
        $orders[$k]['ee'] = $thistatus;
        $orders[$k]['ff'] = $time;
        $orders[$k]['gg'] = $collect['goodsname'];
//			$orders[$k]['h1'] = $add['province'];
//			$orders[$k]['h2'] = $add['city'];
//			$orders[$k]['h3'] = $add['county'];
//			$orders[$k]['h4'] = $add['detailed_address'];
//			$orders[$k]['hh'] = $v['address'];
//			$orders[$k]['ii'] = $v['transid'];
//			$orders[$k]['jj'] = $v['expresssn'];
//			$orders[$k]['kk'] = $v['express'];
//			$orders[$k]['m1'] = $disname;
//			$orders[$k]['m2'] = $v['freight'];
        $orders[$k]['m3'] = $saler['nickname'];
        $orders[$k]['m4'] = $realstore['storename'];
//			$orders[$k]['m5'] = $com['storename'];
//			$orders[$k]['m6'] = $addresstype;
//            $orders[$k]['m8'] = date('Y-m-d H:i:s', $v['hexiaotime']);
        if (!empty($v['hexiaotime'])) {
            $orders[$k]['m8'] = date('Y-m-d H:i:s', $v['hexiaotime']);
        } else {
            $orders[$k]['m8'] = '';
        }
//			$orders[$k]['m9'] = $v['adminremark'];
//			$orders[$k]['ll'] = $v['tuan_id'];
        if (empty($v['successtime'])) {
            $v['successtime'] = time();
        }
//			$orders[$k]['mz'] = date('Y-m-d H:i:s', $v['successtime']);
//			$orders[$k]['mm'] = $v['remark'];
//			$orders[$k]['oo'] = '';
//			$orders[$k]['pp'] = '';

        foreach ($filter as $key => $title) {
            $html .= $orders[$k][$key] . "\t,";
        }
        $html .= "\n";
//            foreach ($col as $c => $cc) {
//                $gname = $cc['goodsname'];
//                $gopname = $cc['item'];
//                $orders[$k]['aa'] = '';
//                $orders[$k]['bb'] = '';
//                $orders[$k]['cc'] = '';
//                $orders[$k]['dd'] = $cc['oprice'];
//                $orders[$k]['ee'] = '';
//                $orders[$k]['ff'] = $time;
//                $orders[$k]['gg'] = $gname;
////					$orders[$k]['h1'] = '';
////					$orders[$k]['h2'] = '';
////					$orders[$k]['h3'] = '';
////					$orders[$k]['h4'] = '';
////					$orders[$k]['hh'] = '';
////					$orders[$k]['ii'] = '';
////					$orders[$k]['jj'] = '';
////					$orders[$k]['kk'] = '';
////					$orders[$k]['m1'] = '';
////					$orders[$k]['m2'] = '';
//                $orders[$k]['m3'] = '';
//                $orders[$k]['m4'] = '';
////					$orders[$k]['m5'] = '';
////					$orders[$k]['m6'] = $addresstype;
////					$orders[$k]['m7'] = '';
//                $orders[$k]['m8'] = '';
////					$orders[$k]['m9'] = '';
////					$orders[$k]['ll'] = '';
////					$orders[$k]['mz'] = '';
////					$orders[$k]['mm'] = '';
////					$orders[$k]['oo'] = $cc['num'];
////					$orders[$k]['pp'] = $gopname;
//
//                foreach ($filter as $key => $title) {
//                    $html .= $orders[$k][$key] . "\t,";
//                }
//                $html .= "\n";
//            }
//        }
        unset($com);
        unset($saler);
        unset($add);
    }
    /* 输出CSV文件 */
    header("Content-type:text/csv");
    header("Content-Disposition:attachment; filename={$str}.csv");
    echo $html;
    exit();
}
include wl_template('order/order');
exit();
